﻿/*
 * @Autor: PK
 * @Date: 2021-03-31 13:34:14
 * @Description: 顾客的抽象类，在Customer基础上设置了默认的一些功能，可以继承该类实现自己的顾客，减少代码
 */
using System;
using System.Collections.Generic;
using ShkSimulation.core.exceptions;
using ShkSimulation.core.template.agency;

namespace ShkSimulation.core.template.customer {
    
    public abstract class AbstractTraveler : Customer {

        public int NextAgencyIndex { get; private set; }
        public Agency LastAgency { get; private set; }
        public Agency CurrentAgency { get; private set; }
        internal IReadOnlyList<Agency> Agencies { get; private set; }
        
        public void SetAgenciesAndCloneAgencies(IReadOnlyList<Agency> agencies) {
            if (agencies == null || agencies.Count == 0) {
                throw new ErrorParamException("顾客服务台不能为空");
            }

            NextAgencyIndex = 0;
            this.Agencies = agencies;
        }

        public override IReadOnlyList<Agency> GetAgencies() {
            return this.Agencies;
        }
        
        public override void EnterNextAgency() {
            if (Agencies == null || Agencies.Count == 0 || NextAgencyIndex >= Agencies.Count) {
                AfterExistNetwok();
                return;
            }
            LastAgency = CurrentAgency;
            CurrentAgency = Agencies[NextAgencyIndex];
            NextAgencyIndex++;
            CurrentAgency.InternalArrive(this);
        }

        public override Agency PeekNextAgency() {
            if (Agencies == null || Agencies.Count == 0 || NextAgencyIndex >= Agencies.Count - 1) {
                return null;
            }
            return Agencies[NextAgencyIndex + 1];
        }

        public override void SkipAgency(int count) {
            if (Agencies == null) {
                throw new ErrorParamException($"服务台为空");
            }
            if (NextAgencyIndex + count >= Agencies.Count) {
                throw new ErrorParamException($"跳过服务台超出索引：{count + NextAgencyIndex} > {Agencies.Count - 1}");
            }
            NextAgencyIndex += count;
        }

        public override void ExistNetwok() {
            if (Agencies != null) {
                NextAgencyIndex = Agencies.Count;
            }
            AfterExistNetwok();
        }

        public virtual void AfterExistNetwok() {
        }
        
    }
}
